/* Includes ------------------------------------------------------------------*/
#include "delay.h"

//#include "platform_config.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

volatile uint32_t __counter_micros;
volatile uint32_t __counter_millis;
volatile uint32_t __countdown_millis;

static uint16_t count = 0;
static int verifica = 0;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
 * @brief  millisecond
 * @param  none
 * @retval None
 */
void TIM2_delaytimer_start(void) {

	TIM_TimeBaseInitTypeDef TimeBaseStructure;

	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_ClocksTypeDef RCC_Clocks;
	RCC_GetClocksFreq(&RCC_Clocks);

	TimeBaseStructure.TIM_Prescaler = (RCC_Clocks.SYSCLK_Frequency>>0)/1000000L ;
	TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TimeBaseStructure.TIM_Period = 1000 - 1;
	TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TimeBaseStructure.TIM_RepetitionCounter = 0;

	//Supply APB1 Clock
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	/* Time base configuration */
	TIM_TimeBaseInit(TIM2, &TimeBaseStructure);

	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);

	/* Enable the TIM2 gloabal Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

	// reset time-passed counters
	__counter_micros = 0;
	__counter_millis = 0;
	TIM_SetCounter(TIM2, 0);
	__countdown_millis = 0;

	/* TIM enable counter */
	TIM_Cmd(TIM2, ENABLE);
}

void reset_count(){
	count = 0;
}

uint32_t millis(void) {
	return __counter_millis;
}

void TIM2_delaytimer_reset() {
	__counter_millis = 0;
	__counter_micros = 0;
	TIM_SetCounter(TIM2, 0);
}


void countdown_start(uint32_t t) {
	__countdown_millis = t;
}

uint8_t countdown_expired(void) {
	return __countdown_millis == 0;
}

void TIM2_IRQHandler(void) {
	if (TIM_GetITStatus(TIM2, TIM_IT_Update ) == SET) {
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
		if ( __countdown_millis > 0 )
			__countdown_millis--;
		__counter_micros += 1000;
		__counter_millis += 1;
	}
	if(millis() == 55248){
		count++;
		lcd_time(count);
		TIM2_delaytimer_reset();
		verifica = 1;
	}
	if(millis() == 10000 && verifica == 1){
		verifica = 0;
		lcd_acionamento(retorna_estado());
	}
}
